<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>allSettled</title>
</head>
<body>
<script>

    function ajax(url) {
        return new Promise((resolve,reject) => {
            let xhr = new XMLHttpRequest();
            xhr.open('GET',url);
            xhr.send();
            xhr.onload = function () {
                if(this.status == 200){
                    resolve(JSON.parse(this.response));
                }else{
                    reject(this);
                }
            }
        });
    }

    /*
    * allSettled用于处理多个promise,只会关注执行完成，并不会关心执行成功
    * allSettled状态只会是fulfilled
    *
    * 下面的p2返回状态为rejected，但是promise.allSettled并不关心,只是将状态设置为fulfilled
    * */
    // const p1 = new Promise((resolve,reject) => {
    //     resolve("resolved");
    // });
    //
    // const p2 = new Promise((resolve,reject) => {
    //     reject("rejected");
    // });
    //
    // Promise.allSettled([p1,p2]).then(msg => {
    //     console.log(msg);
    // });

    //下面是获取用户信息,但是并不关注某个用户是否获取不成功
    // const api = "http://localhost/basicjs/promise/php";
    //
    // const promises = [
    //     ajax(`${api}/user.php?name=向军`),
    //     ajax(`${api}/user.php?name=后盾人1`)
    // ];
    // Promise.allSettled(promises).then(response => {
    //     console.log(response);
    // });







</script>
</body>
</html>